import { Attributes } from './attribute';
let _Attributes = Attributes;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import AttributeMock from './mallm/pub/attributeMock';
 * AttributeMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/attribute/listKey').reply(config => {
      let params = config.params;
      let mockAttributes = _Attributes.filter(attribute => {
      	if(params.id||params.id==''){ if( attribute.id.indexOf(params.id)>=0) return true;}
      	if(params.attributeCategoryId||params.attributeCategoryId==''){ if( attribute.attributeCategoryId.indexOf(params.attributeCategoryId)>=0) return true;}
      	if(params.name||params.name==''){ if( attribute.name.indexOf(params.name)>=0) return true;}
      	if(params.inputType||params.inputType==''){ if( attribute.inputType.indexOf(params.inputType)>=0) return true;}
      	if(params.sortOrder||params.sortOrder==''){ if( attribute.sortOrder.indexOf(params.sortOrder)>=0) return true;}
      	if(params.value||params.value==''){ if( attribute.value.indexOf(params.value)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( attribute.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockAttributes
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/attribute/list').reply(config => {
      let params = config.params;
      let mockAttributes = _Attributes.filter(attribute => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(attribute.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.attributeCategoryId||params.attributeCategoryId==''){if(attribute.attributeCategoryId.indexOf(params.attributeCategoryId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.name||params.name==''){if(attribute.name.indexOf(params.name)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.inputType||params.inputType==''){if(attribute.inputType.indexOf(params.inputType)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sortOrder||params.sortOrder==''){if(attribute.sortOrder.indexOf(params.sortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.value||params.value==''){if(attribute.value.indexOf(params.value)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(attribute.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockAttributes
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockAttributes.length;
      mockAttributes = mockAttributes.filter((attribute, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockAttributes,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/attribute/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Attributes = _Attributes.filter(attribute => attribute.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/attribute/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Attributes = _Attributes.filter(attribute => !params.includes(attribute.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/attribute/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,attributeCategoryId,name,inputType,sortOrder,value,deptid } = params;
      _Attributes.some(attribute => {
        if (attribute.id === id) {
			attribute.id = id;
			attribute.attributeCategoryId = attributeCategoryId;
			attribute.name = name;
			attribute.inputType = inputType;
			attribute.sortOrder = sortOrder;
			attribute.value = value;
			attribute.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/attribute/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,attributeCategoryId,name,inputType,sortOrder,value,deptid } = params;
      _Attributes.push({
		id:id,
		attributeCategoryId:attributeCategoryId,
		name:name,
		inputType:inputType,
		sortOrder:sortOrder,
		value:value,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};